//路由鉴权
import router from '@/router'
import nprogress from 'nprogress' //引入进度条插件
import 'nprogress/nprogress.css'
import useUserStore from './store/modules/user'

//全局前置守卫
router.beforeEach((to, _from, next) => {
    nprogress.start()
    let userStore = useUserStore()
    let token = userStore.token
    if (token) {
        //用户登入后有token，访问login就去首页
        if (to.path === '/login') {
            next('/')
        } else {
            next()
        }
    } else {
        //用户未登入token，访问任何页面都只能去登录页
        if (to.path === '/login') {
            next()
        } else {
            next({ path: '/login', query: { redirect: to.path } })
        }
    }
})

//全局后置守卫
router.afterEach((to, from) => {
    document.title = to.meta.title as string
    nprogress.done()
})
